package com.xy.redisson.service.impl;

import com.xy.redisson.constant.GlobalConstant;
import com.xy.redisson.properties.RedissonProperties;
import com.xy.redisson.service.RedissonConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.config.Config;

/**
 * 单机部署Redisson配置
 * @author xxy
 * @date 2022/1/5 21:08
 */
@Slf4j
public class StandaloneConfigImpl implements RedissonConfigService {
    @Override
    public Config createRedissonConfig(RedissonProperties redissonProperties) {
        Config config = new Config();
        try {
            String address = redissonProperties.getAddress();
            String password = redissonProperties.getPassword();
            int database = redissonProperties.getDatabase();
            String redisAddress = GlobalConstant.REDIS_CONNECTION_PREFIX.getConstant_value() + address;
            config.useSingleServer().setAddress(redisAddress);
            config.useSingleServer().setDatabase(database);
            if (StringUtils.isNotBlank(password)){
                config.useSingleServer().setPassword(password);
            }
        } catch (Exception e) {
            log.error("单机部署 Redisson init error", e);
        }
        return config;
    }
}
